package com.test;

import java.util.ArrayList;
import java.util.List;

public class Shop {

    public static void main(String[] args) {
        BuyFruit buyFruit = new BuyFruit();
        //苹果 买1个
        Fruit apple = new Apple(1);
        Fruit strawberry = new Strawberry(1);

        //第一题计算总价 假设都买1个
        List<Fruit> fruitList = new ArrayList<>();
        fruitList.add(apple);
        fruitList.add(strawberry);

        double totalMoney = buyFruit.buy(fruitList);

        System.out.println("第一题 假设都买1个 = " + totalMoney);

        //第二题 加入芒果20单价
        Fruit mango = new Mango(1);
        fruitList.add(mango);
        double secondTotalMoney = buyFruit.buy(fruitList);
        System.out.println("第二题假设都买一个 = " + secondTotalMoney);

        //第三题 折扣 草莓折扣 打8折
        List<Fruit> discountFruitList = new ArrayList<>();
        Fruit appleNew = new Apple(1);
        Fruit discountStrawberry = new Strawberry(1,0.8);
        Fruit mangoNew = new Mango(10);

        discountFruitList.add(appleNew);
        discountFruitList.add(discountStrawberry);
        discountFruitList.add(mangoNew);

        double totalMoneyNew = buyFruit.buy(discountFruitList);
        System.out.println("第三题 假设都买一个 = " + totalMoneyNew);

        //第四题
        Promotion promotion = new Promotion();
        double promotionMoney = promotion.F1(totalMoneyNew);

        System.out.println("第四题促销后总额 = " + promotionMoney);
    }

}

//买水果
class BuyFruit{


    //接收水果对象
    public double buy(List<Fruit> fruits){
        double totalMoney = 0;
        for (Fruit fruit : fruits) {
            //计算每一种的价格
            double totalTemp = fruit.getTotalMoney(fruit);
            totalMoney = totalMoney + totalTemp;
        }
        return totalMoney;
    }
}

//水果类
abstract class Fruit{
    //价钱
    double price;
    //水果数量
    int fruitNum;
    //折扣力度默认为1
    double discount = 1.0;

    //total
    public abstract double getTotalMoney(Fruit f);

}

//苹果
class Apple extends Fruit{
    Apple(int num){
        //单价8的
        super.price = 8;
        //购买数量
        super.fruitNum = num;
    }
    //当有折扣时使用
    Apple(int num,double discount){
        //单价8的
        super.price = 8;
        //购买数量
        super.fruitNum = num;
        //折扣
        super.discount = discount;
    }

    @Override
    public double getTotalMoney(Fruit f) {
        return f.price * f.fruitNum * f.discount ;
    }
}

//草莓
class Strawberry extends Fruit{
    Strawberry(int num){
        //单价13的
        super.price = 13;
        //购买数量
        super.fruitNum = num;
    }
    //当有折扣时使用
    Strawberry(int num,double discount){
        //单价13的
        super.price = 13;
        //购买数量
        super.fruitNum = num;
        //折扣
        super.discount = discount;
    }

    @Override
    public double getTotalMoney(Fruit f) {
        return f.price * f.fruitNum * f.discount;
    }
}

//芒果
class Mango extends Fruit{
    Mango(int num){
        //单价20的
        super.price = 20;
        //购买数量
        super.fruitNum = num;
    }

    Mango(int num,double discount){
        //单价20的
        super.price = 20;
        //购买数量
        super.fruitNum = num;
        //折扣
        super.discount = discount;
    }

    @Override
    public double getTotalMoney(Fruit f) {
        return f.price * f.fruitNum * f.discount;
    }
}

class Promotion{

    double num = 0;
    //促销规则 每满100 - 10
    //折扣方法1
    public double F1(double totalMoney){
        int num = (int)totalMoney / 100;

        System.out.println("促销减少额度 = " + num);

        //计算最终价格
        return totalMoney - num * 10;
    }
}